package com.liangdemo.web.thread;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Name LiangErLe
 * @date 2022/6/16 14:00
 */
public class LockThread implements Runnable {
    private Integer key = 0;


    Lock lock = new ReentrantLock();

    public void run() {
        while (key < 100) {
            try {
                lock.lock();
                test();
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }
    }

    public void test() {
        System.out.println(Thread.currentThread().getName() + ":" + key);
        key++;
    }

    public static void main(String[] args) {
        LockThread lockThread = new LockThread();

        new Thread(lockThread, "线程1").start();

        new Thread(lockThread, "线程2").start();
    }
}
